﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CardCharger.lib;

namespace CardCharger.forms
{
    public partial class IssueSetting : Form
    {

        UserCardIssue.t_GCZ_CardInfo old = new UserCardIssue.t_GCZ_CardInfo();

        public IssueSetting()
        {
            InitializeComponent();
            this.VisibleChanged += IssueSetting_VisibleChanged;
        }

        void IssueSetting_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible == true)
            {
                old = Get();
            }
        }

        /// <summary>
        /// 从输入取得设置值
        /// </summary>
        /// <returns></returns>
        UserCardIssue.t_GCZ_CardInfo Check()
        {
            UserCardIssue.t_GCZ_CardInfo info = new UserCardIssue.t_GCZ_CardInfo();
            try {

                info.CardID = UInt64.Parse(tb_CardId.Text, System.Globalization.NumberStyles.HexNumber);
                info.Key = UInt64.Parse(tb_PW.Text, System.Globalization.NumberStyles.HexNumber);
                info.PileAddr = UInt64.Parse(tb_Addr.Text, System.Globalization.NumberStyles.HexNumber);
                double sum = double.Parse(tb_Money.Text, System.Globalization.NumberStyles.Float);
                info.Sum = (UInt32)(sum * 100);
                tb_Money.Text = string.Format("{0:0.00}",(((double)(info.Sum)) / 100));
                info.GunNum = byte.Parse(tb_Gun.Text, System.Globalization.NumberStyles.HexNumber);
                info.Lock = (byte)(comboBox1.SelectedIndex);
                return info;   
            }
            catch { return null; }
        }

        /// <summary>
        /// 取得配置
        /// </summary>
        /// <returns></returns>
        public UserCardIssue.t_GCZ_CardInfo Get() {
            UserCardIssue.t_GCZ_CardInfo info;
            try { 
                info = Check();
                if (info == null) return new UserCardIssue.t_GCZ_CardInfo(); 
                return info; 
            }
            catch { 
                info = new UserCardIssue.t_GCZ_CardInfo(); 
                return info;
            }
        }

        public void Set(UserCardIssue.t_GCZ_CardInfo info)
        {
            try {
                tb_CardId.Text = String.Format("{0:X16}", info.CardID);
                tb_PW.Text = String.Format("{0:X16}", info.Key);
                tb_Addr.Text = String.Format("{0:X16}", info.PileAddr);
                tb_Money.Text = String.Format("{0:0.00}", ((double)info.Sum) / 100);
                tb_Gun.Text = String.Format("{0:X2}",info.GunNum);
                comboBox1.SelectedIndex = (int)info.Lock;
            }
            catch { }
        }

        private void b_Cancel_Click(object sender, EventArgs e)
        {
            Set(old);
            this.Hide();
        }

        private void b_Confirm_Click(object sender, EventArgs e)
        {
            try {

                UserCardIssue.t_GCZ_CardInfo c = Check();
                if (c == null) throw new Exception("设置不正确");
            }
            catch { 
                MessageBox.Show("设置不正确");
                return;
            }                
            old = Get();
            this.Hide();  
        }
    


    }
}
